# coding: utf8

import os
import logging
import time
import re
import json
from lxml import etree
import traceback
import requests_html
from multiprocessing.pool import ThreadPool


"""厦门大学
电子科学与技术学院（国家示范性微电子学院）
"""


logging.basicConfig(level=logging.INFO)

session = requests_html.HTMLSession()
pool = ThreadPool(os.cpu_count() * 2)


def teacher_list():
    urls = [
        "https://ese.xmu.edu.cn/teacherList.jsp?urltype=tree.TreeTempUrl&wbtreeid=1071",  # 教授
        "https://ese.xmu.edu.cn/teacherList.jsp?urltype=tree.TreeTempUrl&wbtreeid=1072",  # 副教授
        "https://ese.xmu.edu.cn/teacherList.jsp?urltype=tree.TreeTempUrl&wbtreeid=1073",  # 助理教授
        "https://ese.xmu.edu.cn/teacherList.jsp?urltype=tree.TreeTempUrl&wbtreeid=1063",  # 技术支撑团队
    ]
    urls = urls[::-1]

    teachers = []
    while urls:
        url = urls.pop()

        logging.info("processing page: %s", url)
        try:
            r = session.get(url)
            if r.status_code == 200:
                next_url = r.html.xpath("//span[@class='p_pages'][1]/span[contains(@class, 'p_next p_fun')][1]/a[1]/@href")
                next_url = "https://ese.xmu.edu.cn/teacherList.jsp" + next_url[0] if next_url else None
                urls.append(next_url) if next_url else None

                lis = r.html.xpath("//div[@class='infoContent'][1]//div[@class='container-fluid'][1]//div[@class='row'][1]//*[contains(@class, 'pic-item')]")
                for li in lis:
                    avatar = li.xpath("//div[@class='sz-img'][1]/img[1]/@src")
                    avatar = "https://ese.xmu.edu.cn" + avatar[0] if avatar else None
                    name = li.xpath("//div[@class='sz-con'][1]/div[@class='sz-name'][1]/a[1]/text()")[0]
                    teacher_url = li.xpath("//div[@class='sz-con'][1]/div[@class='sz-name'][1]/a[1]/@href")
                    teacher_url = "https://ese.xmu.edu.cn/" + teacher_url[0] if teacher_url else None
                    pos = li.xpath("//div[@class='sz-con'][1]/div[@class='sz-info'][1]/div[1]/text()")
                    pos = pos[0] if pos else ''
                    ri = li.xpath("//div[@class='sz-con'][1]/div[@class='sz-info'][1]/div[2]/text()")
                    ri = ri[0] if ri else ''

                    # logging.info("name: %s, pos: %s, ri: %s, avatar: %s, url: %s", name, pos, ri, avatar, teacher_url)
                    teachers.append((name, teacher_url))
            else:
                r.raise_for_status()

        except Exception as e:
            traceback.print_exc()
            raise e

    pool.imap_unordered(teacher_detail, teachers)
    pass


dep_ptn = re.compile(r"系别[:：]")
pos_ptn = re.compile(r"职称[:：]")
email_ptn = re.compile(r"邮箱[:：]")
tel_ptn = re.compile(r"联系方式[:：]")
addr_ptn = re.compile(r"办公地点[:：]")

degree_ptn = re.compile(r"学历[:：]\n([\s\S]+?\n{2,})")
affi_ptn = re.compile(r"学术兼职[:：]\n([\s\S]+?\n{2,})")
ri_ptn = re.compile(r"研究方向[:：]\n([\s\S]+?\n{2,})")


def teacher_detail(payload):
    name, url = payload

    try:
        r = session.get(url)
        if r.status_code == 200:
            basic_info = r.html.xpath("//div[contains(@class, 'row expert-row')][1]")[0]
            avatar = basic_info.xpath("//img[1]//@src")
            avatar = "https://ese.xmu.edu.cn" + avatar[0] if avatar else None

            name = basic_info.xpath("//*[@class='expert-name'][1]/text()")[0]
            dep = basic_info.xpath("//p[1]")[0].full_text
            pos = basic_info.xpath("//p[2]")[0].full_text
            email = basic_info.xpath("//p[3]")[0].full_text
            tel = basic_info.xpath("//p[4]")[0].full_text
            addr = basic_info.xpath("//p[5]")[0].full_text

            dep, pos, email, tel, addr = [ptn.sub("", i) for ptn, i in
                                          zip((dep_ptn, pos_ptn, email_ptn, tel_ptn, addr_ptn),
                                              (dep, pos, email, tel, addr), )]

            intro_e = r.html.xpath("//*[@id='vsb_content'][1]/div[@class='v_news_content'][1]/*")

            intro = []
            temp = []
            for e in intro_e:
                if e.tag == "h2" or e.xpath("//strong"):
                    intro.append("\n\n" + "\n".join(temp))
                    temp = []
                temp.append(e.full_text)
            intro.append("\n" + "\n".join(temp))
            intro = "".join(intro)

            degree, affi, ri = [ptn.search(intro) for ptn in (degree_ptn, affi_ptn, ri_ptn)]
            degree, affi, ri = [(match.group(1).strip() if match else None) for match in (degree, affi, ri)]

            logging.info("name: %s, dep: %s, pos: %s, email: %s, tel: %s, addr: %s, degree: %s, affi:%s, ri: %s, avatar: %s",
                         name, dep, pos, email, tel, addr, degree, affi, ri, avatar)
            logging.info("")
            logging.info("")
            pass
        else:
            r.raise_for_status()
    except Exception as e:
        traceback.print_exc()
        raise e


if __name__ == '__main__':
    teacher_list()

    pool.close()
    pool.join()

    session.close()





